package com.yj.suncity.async;

import java.io.IOException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.yj.suncity.utis.HttpUtil;
import com.yj.suncity.utis.ProgressUtil;
import com.yj.suncity.utis.PullToRefreshView;
import com.yj.suncity.utis.ToastUtil;


import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;

public class AsyncTaskUtil extends AsyncTask<String, Void, String> {

	private ResultCallBack resultCallBack;
	private ProgressUtil progressUtil;
	private List<NameValuePair> pairs;
	private ProgressBar progressBar;
	private boolean cancelable;
	private boolean mRefreshFlag;
    private String message;
	private Context context;
	private PullToRefreshView mPullToRefreshView;
	private static final int IO_EXCEPTION = 1;
	private static final int JSON_EXCEPTION = 2;
	
	private  Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case IO_EXCEPTION:
				ToastUtil.ShowToast(context, "网络异常！");
				break;
			case JSON_EXCEPTION:
				ToastUtil.ShowToast(context, "数据解析异常！");
				break;
			default:
				break;
			}
		};
	};

	
	
	
	/**
	 * 异步加载
	 * @param context 上下文对象
	 * @param pairs 参数
	 * @param mPullToRefreshView 刷新view
	 * @param mRefreshFlag 是否是刷新
	 * @param progressBar  圆形进度条
	 * @param cancelable 对话框进度条是否可以取消
	 * @param message 进度对话框提示语
	 * @param resultCallBack 返回jsonObject
	 */
	public AsyncTaskUtil(Context context,List<NameValuePair> pairs,PullToRefreshView mPullToRefreshView,
			boolean mRefreshFlag,
			ProgressBar progressBar,boolean cancelable,String message, ResultCallBack resultCallBack) {
		super();
		this.resultCallBack = resultCallBack;
		this.pairs = pairs;
		this.progressBar = progressBar;
		this.context=context;
		this.cancelable=cancelable;
		this.message=message;
		this.mPullToRefreshView=mPullToRefreshView;
		this.mRefreshFlag=mRefreshFlag;

	}

	@Override
	protected void onPreExecute() {
		if (mRefreshFlag) {
			
		}else{
			if (progressBar != null) {
				progressBar.setVisibility(View.VISIBLE);
			} else {
				progressUtil = new ProgressUtil();
				progressUtil.ShowProgress(context, true, cancelable, message);
			}
		}
		super.onPreExecute();
	}

	@Override
	protected String doInBackground(String... params) {
		try {
			String json;
			if (pairs == null) {
				json = HttpUtil.HttpClientDoGet(params[0]);
			} else {
				json = HttpUtil.HttpClientdoPost(params[0], pairs);
			}
			return json;
		} catch (IOException e) {
			Message message = new Message();
			message.what = IO_EXCEPTION;
			handler.sendMessage(message);
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		if (mRefreshFlag) {
			mPullToRefreshView.onHeaderRefreshComplete();
			mPullToRefreshView.onFooterRefreshComplete();
		} else {
			if (progressBar != null) {
				progressBar.setVisibility(View.GONE);
			} else {
				progressUtil.ShowProgress(context, false, cancelable, message);
			}
		}
		if (result != null) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				resultCallBack.JSONResult(jsonObject);
			} catch (JSONException e) {
				Message message = new Message();
				message.what = JSON_EXCEPTION;
				handler.sendMessage(message);
				e.printStackTrace();
			}
		} else {
			ToastUtil.ShowToast(context, "没有更多数据！");
		}
		super.onPostExecute(result);
	}

	public interface ResultCallBack {
		void JSONResult(JSONObject jsonObject);
	}
}
